import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/match/req_match.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/config/enum/load_state.dart';
import 'package:flutter_changdian_football/model/match/match_detail_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_changdian_football/utils/share_util.dart';
import 'package:flutter_changdian_football/widget/sheet/share_icon_sheet.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class EnrollmentDetailsController extends GetxController {

  EnrollmentDetailsController({
    required this.clubId,
    required this.matchId
  });

  final String clubId;
  final String matchId; 

  @override
  void onInit() {
    super.onInit();

    initData();

    eventBus.on<BottomNavigationBarEvent>().listen((event) {
      if (event.message == CustomConstant.EVENT_MATCH_REFRESH) {
        EasyLoading.show();
        // 刷新数据
        initData();
      } 
    });
  }


  LoadState state = LoadState.loading;

  MatchDetailModel? matchDetailModel;

  initData() async {
    var resp = await ReqMatch.matchDetail(clubId: clubId, matchId: matchId);
    if (resp.status == Status.completed) {
      if (resp.data['code'] == '200') {
        matchDetailModel = MatchDetailModel.fromJson(resp.data['data']);

        if (resp.data['data'] != null) {
          state = LoadState.success;
        } else {
          state = LoadState.empty;
        }

        update();
      } else {
        CustomToast.showTextToast('${resp.data['msg']}');
        state = LoadState.error;
        update();
      }
    } else {
      state = LoadState.network;
      update();
    }
  }

    /// 分享
  getSharePage() async {
    try {
      EasyLoading.show();
      var resp = await ReqOther.getSharePage(sharePageName: CustomConstant.SHARE_MATCH, clubId:'${clubId}', matchId: '$matchId');

      if (resp.data['code'] == '200') {
        // 分享推荐
        ShareIconSheet.shareIcon(
          null, 
          null, 
          resp.data['data']['url'], 
          null,
          isShowImg: false,
          wxTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "0");
          },
          pyqTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "1");
          },
          qqTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "2");
          },
          wbTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "3");
          },
        );
      } else {
        CustomToast.showTextToast(resp.data['msg']);
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

/// 比赛报名
  signUp(matchId) async {
    try {
      EasyLoading.show();

      var resp = await ReqMatch.signUp(
        matchId: '$matchId'
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('操作成功');
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_MATCH_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 取消报名
  cancelSignUp(matchId) async {
    try {
      EasyLoading.show();

      var resp = await ReqMatch.cancelSignUp(
        matchId: '$matchId'
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('操作成功');
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_MATCH_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

}